package com.iris.live.services.common.reconsitution;

import com.iris.live.services.common.StringUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 类转换（反射）
 * Created by yitianle
 * 2017/4/5.
 */
public class BeanUtils {
    /**
     * map 转 Bean
     * @param map
     * @param beanClass
     * @return
     *
     * @throws Exception
     */
    public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
        if (map == null)
            return null;
        Object obj = beanClass.newInstance();
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            int mod = field.getModifiers();
            if(Modifier.isStatic(mod) || Modifier.isFinal(mod)||!map.containsKey(field.getName())){    //防止map中没有bean属性
                continue;
            }
            field.setAccessible(true);
            field.set(obj, map.get(field.getName()));
        }
        return obj;
    }


    /**
     * 适用BeanWrapper实现 map 转 Bean
     * @param map
     * @param beanClass
     * @return
     *
     * @throws Exception
     */
    public static Object mapToWrappedObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
        if (map == null)
            return null;

        BeanWrapper bw = new BeanWrapperImpl(beanClass);
        Set<String> properties = map.keySet();
        for(String property : properties){
            Object value = map.get(property);
            if(StringUtils.isNullOrBlank(value)) continue;

            Class<?> type  = bw.getPropertyType(property);
            if (null == type) continue;  // 去除非实体类属性
            if(type.equals(Date.class)){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                bw.setPropertyValue(property,  new java.sql.Date(sdf.parse(value.toString()).getTime()));
            } else if(type.equals(Integer.class)) {
                int index = map.get(property).toString().indexOf(".");
                if (-1 != index) {
                    // 若包含小数点的话，则去掉小数点
                    bw.setPropertyValue(property, value.toString().substring(0,index));
                } else {
                    bw.setPropertyValue(property, value.toString());
                }
            }
            else{
                bw.setPropertyValue(property, value);
            }
        }

        return bw.getWrappedInstance();
    }

    /**
     * 适用beanWrapper map 转 Bean 批量
     * @param mapList
     * @param beanClass
     * @return
     * @throws Exception
     */
    public static List<Object> mapToWrapperObjectList(List<Map<String, Object>> mapList, Class<?> beanClass) throws Exception {
        List<Object>  objectList = new ArrayList<Object>();
        if (null == mapList || mapList.size() <= 0){
            return null;
        }
        for (Map map:mapList) {
            Object obj = mapToWrappedObject(map,beanClass);//  复用代码
            objectList.add(obj);
        }
        return objectList;
    }

    /**
     * map 转 Bean 批量
     * @param mapList
     * @param beanClass
     * @return
     * @throws Exception
     */
    public static List<Object> mapToObjectList(List<Map<String, Object>> mapList, Class<?> beanClass) throws Exception {
        List<Object>  objectList = new ArrayList<Object>();
        if (null == mapList || mapList.size() <= 0){
            return null;
        }
        for (Map map:mapList) {
            Object obj = mapToObject(map,beanClass);//  复用代码
            objectList.add(obj);
        }
        return objectList;
    }

   /* public static void main(String[] args) throws Exception{
        Map<String,Object> map = Maps.newHashMap();
        Map<String,Object> map2 = Maps.newHashMap();
        List<Map<String,Object>> mapList =  new ArrayList<Map<String,Object>>();
        mapList.add(map);
        mapList.add(map2);
        SelectBoxOptionVo vo  = (SelectBoxOptionVo)mapToObject(map,SelectBoxOptionVo.class);
        System.out.println("ID" + vo.getOptionId() );
        System.out.println("选项名称" + vo.getOptionName());
        List<Object> objList =  mapToObjectList(mapList,SelectBoxOptionVo.class);
        for (Object obj:objList) {
            SelectBoxOptionVo vo2 = (SelectBoxOptionVo)obj;
            System.out.println("ID" + vo2.getOptionId() );
            SelectBoxOptionVo.out.println("选项名称" + vo2.getOptionName());
        }
    }*/
}
